package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.ChangePsRespnse;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.ChangePsPresenter;
import com.topboya.linker.ui.view.ChangePsView;
import com.topboya.linker.utils.UIUtils;

public class ChangepsActivity extends BaseActivity<ChangePsView,ChangePsPresenter> implements ChangePsView {

    private static final String TAG = "ChangepsActivity";
    private Toolbar toolbar;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private EditText et_old_ps;
    private EditText et_new_ps;
    private EditText et_sure_ps;
    private Button btn_update;
    private CheckBox ck_old_ps;
    private CheckBox ck_new_ps;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_change_ps;
    }

    @Override
    protected void initView() {

        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        et_old_ps = findViewById(R.id.et_old_ps);
        et_new_ps = findViewById(R.id.et_new_ps);
        et_sure_ps = findViewById(R.id.et_sure_ps);
        btn_update = findViewById(R.id.btn_update);
        ck_old_ps = findViewById(R.id.ck_old_ps);
        ck_new_ps = findViewById(R.id.ck_new_ps);
        iniTitle();

    }

    private void iniTitle() {

         toolbar_title.setText("修改密码");
         tv_toolbar_right.setVisibility(View.GONE);

    }

    @Override
    protected void initData() {


    }

    @Override
    protected void initListener() {

        toolbar.setNavigationOnClickListener(v -> { finish(); });
        btn_update.setOnClickListener(v -> { changePassword(); });
        ck_new_ps.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){

                et_new_ps.setTransformationMethod(PasswordTransformationMethod.getInstance());

            }else {
                et_new_ps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

            }
            et_new_ps.setSelection(et_new_ps.getText().length());
        });
        ck_old_ps.setOnCheckedChangeListener((buttonView, isChecked) -> {

            if (isChecked){

                et_old_ps.setTransformationMethod(PasswordTransformationMethod.getInstance());

            }else {
                et_old_ps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
            et_old_ps.setSelection(et_old_ps.getText().length());

        });

    }

    private void changePassword() {

        String oldps = et_old_ps.getText().toString().trim();
        String newps = et_new_ps.getText().toString().trim();
        String sureps = et_sure_ps.getText().toString().trim();
        if (TextUtils.isEmpty(oldps)){ UIUtils.showToast("请先输入旧密码");return; }
        if (TextUtils.isEmpty(newps)){UIUtils.showToast("请先输入新密码");return;}
        if (TextUtils.isEmpty(sureps)){UIUtils.showToast("请先输入确认密码");return;}
        if (!newps.equals(sureps)){UIUtils.showToast("两次密码输入不一致");return;}
         mPresenter.modifyPassWord(oldps,newps);

    }

    @Override
    protected ChangePsPresenter createPresenter() {

        return new ChangePsPresenter(this);
    }

    @Override
    public void modifyPassWord(ChangePsRespnse changePsRespnse) {

//        调到登录页面，登录页面开发中
        UIUtils.showToast("修改密码成功");
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }
}
